package com.etc.reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: knowno
 * @create: 2024-09-18 14:14
 * @Version 1.0
 **/
public class TestConstructor {
    public static void main(String[] args) throws NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Class<Person> personClass = Person.class;

        //1 无参数构造
        Constructor<Person> c0 = personClass.getDeclaredConstructor();

        //调用
        Person person0 = c0.newInstance();
        System.out.println(person0);


        //2 构造中，带有一个整数类型的参数的那个方法,私有的
        Constructor<Person> c1 = personClass.getDeclaredConstructor(Integer.class);

        c1.setAccessible(true);
        //调用
        Person person1 = c1.newInstance(100);
        System.out.println(person1); // id =100

        //3  受保护
        //构造中，带有一个整数类型的参数的那个方法
        Constructor<Person> c2 = personClass.getDeclaredConstructor(String.class);

        //调用
        Person person2 = c2.newInstance("男");
        System.out.println(person2); // sex ="男"


    }
}
